<template>
  <div  v-if="ready" class="l-rblock" style="padding: 16px">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="right"
      labelWidth="80px"
      ref="form"
    >
      <el-form-item :label="$t('单位类型')">
        <el-input readonly :value="row.typeName" />
      </el-form-item>
      <el-form-item :label="$t('基准单位')">
        <el-input readonly :value="row.baseUnit.f_name" />
      </el-form-item>
      <el-form-item :prop="item.f_id" v-for="(item,index) in units" :key="index" :label="`1${$t(row.baseUnit.f_name)}=`">
        <el-input type="number" v-model="formData[item.f_id]" >
          <template slot="append">{{item.f_name}}</template>
        </el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
const api = window.$api.erpcase.unitconvert;
export default {
  data() {
    return {
      formData: {
      },
      rules: {
      },
      row:{},
      ready:false
    };
  },
  computed: {
    units(){
      return this.row.units.filter(t=>t.f_isstandard == 1)
    }
  },
  mounted() {},
  methods: {
    // 重置表单
    resetForm() {
      this.ready = false;
      this.formData = {};
      this.rules = {};
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }
      return true;
    },
    // 设置表单数据
    setForm(data) {

      this.row = this.$deepClone(data)
      this.units.forEach(item => {
        const valueObj = this.row.list.find(t=>t.f_ShiftUnitId == item.f_id )
        if(valueObj){
          this.$set(this.formData,item.f_id,valueObj.f_Quantity)
        }
        else{
          this.$set(this.formData,item.f_id,undefined)
        }
        this.rules[item.f_id] = [{required: true, message: "请输入" }]
      });

      this.ready = true   
    },
    // 获取表单数据
    getForm() {
      const postData = {
        caseErpUnitconvertEntity:{
          f_Id:this.row.converId,
          f_UnitTypeId:this.row.typeId
        },
        caseErpUnitconvertdetailList:[]
      }
      for(let key in this.formData) {
        postData.caseErpUnitconvertdetailList.push({
          f_UnitConvertId:this.row.converId,
          f_UnitId:this.row.baseUnit.f_id,
          f_ShiftUnitId:key,
          f_Quantity:this.formData[key]
        })
      }
      console.log(postData,'postData')
      return postData;
    },

    async saveForm({ keyValue }) {
      const postData = this.getForm();
      if (keyValue) {
        // 编辑
        return await this.$awaitWraper(api.update(keyValue, postData));
      } else {
        return await this.$awaitWraper(api.add(postData));
      }
    },
  },
};
</script>